package com.ghy.springbootnacos.nacosconfig;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class TestController {

    @NacosValue ( value = "${info:默认值}",autoRefreshed = true)
    private String info;

    @NacosInjected
    private NamingService namingService;  //服务注册功能的Service
    //查询注册信息
    @GetMapping("/get")
    public String get() throws NacosException {

       // return info;
        return namingService.getAllInstances ( "SpringBoot-Nacos" ).toString ();
    }


    //注册的代码
    @PostMapping("/registry")
    public String registry() throws NacosException {
        //通过Instance注册服务
        Instance instance=new Instance ();
        instance.setClusterName("TestCluster");  //集群名称
        instance.setEnabled(true);   //是否启用
        instance.setEphemeral(true);  //临时节点/持久化节点, CP（Raft）, AP（Distro）
        instance.setIp("localhost");
        instance.setPort(8848);
        instance.setWeight(10);  //1~100
        namingService.registerInstance("SpringBoot-Nacos",instance);
        return "SUCCESS";
    }

}
